﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Sispe.Entities;
using System.Data;

namespace Sispe.Repository
{
    public class ClienteRepository:MasterRepository,IClienteRepository
    {
        public List<CLIENTE> GetAllFromCliente()
        {
            var query = from c in context.CLIENTEs
                        select c;
            return query.ToList();
        }

        public List<CLIENTE> GetAllFromClienteByDescripcion(string descripcion)
        {
            var query = from c in context.CLIENTEs
                        select c;
            if (descripcion != null && descripcion != string.Empty && descripcion != "")
            {
                query = from c in context.CLIENTEs
                        where c.Ruc.ToUpper().Contains(descripcion) ||
                                   c.RazonSocial.ToUpper().Contains(descripcion)
                        select c;
            }
            return query.ToList();
        }

        public CLIENTE GetFromClienteById(int id)
        {
            var query = from c in context.CLIENTEs
                        where c.IdCliente.Equals(id)
                        select c;
            return query.SingleOrDefault();
        }

        public bool ExisteDuplicidad(string ruc, string razonSocial, int id)
        {
            var query = from c in context.CLIENTEs
                        where (c.Ruc.ToUpper().CompareTo(ruc.ToUpper()) == 0 ||
                               c.RazonSocial.ToUpper().CompareTo(razonSocial.ToUpper()) == 0)
                                  && c.IdCliente != id
                        select c;
                            
            return query.Any();
        }

        public void AddCliente(CLIENTE cliente)
        {
            cliente.PROYECTOS = null;
            context.CLIENTEs.Add(cliente);
            try
            {
                context.SaveChanges();
            }
            catch (System.Data.Entity.Validation.DbEntityValidationException e)
            {

                var outputLines = new List<string>();
                foreach (var eve in e.EntityValidationErrors)
                {
                    outputLines.Add(string.Format(
                        "{0}: Entity of type \"{1}\" in state \"{2}\" has the following validation errors:",
                        DateTime.Now, eve.Entry.Entity.GetType().Name, eve.Entry.State));
                    foreach (var ve in eve.ValidationErrors)
                    {
                        outputLines.Add(string.Format(
                            "- Property: \"{0}\", Error: \"{1}\"",
                            ve.PropertyName, ve.ErrorMessage));
                    }
                }
                if (System.IO.File.Exists(@"D:\errors.txt"))
                {
                    System.IO.File.Delete(@"D:\errors.txt");
                    System.IO.File.AppendAllLines(@"d:\errors.txt", outputLines);
                }
                else
                {
                    System.IO.File.AppendAllLines(@"d:\errors.txt", outputLines);
                }
                throw;
            }
        }

        public void UpdateCliente(CLIENTE cliente)
        {
            cliente.PROYECTOS = null;
            context.Entry(cliente).State = System.Data.EntityState.Modified;
            try
            {
                context.SaveChanges();
            }
            catch (System.Data.Entity.Validation.DbEntityValidationException e)
            {

                var outputLines = new List<string>();
                foreach (var eve in e.EntityValidationErrors)
                {
                    outputLines.Add(string.Format(
                        "{0}: Entity of type \"{1}\" in state \"{2}\" has the following validation errors:",
                        DateTime.Now, eve.Entry.Entity.GetType().Name, eve.Entry.State));
                    foreach (var ve in eve.ValidationErrors)
                    {
                        outputLines.Add(string.Format(
                            "- Property: \"{0}\", Error: \"{1}\"",
                            ve.PropertyName, ve.ErrorMessage));
                    }
                }
                if (System.IO.File.Exists(@"D:\errors.txt"))
                {
                    System.IO.File.Delete(@"D:\errors.txt");
                    System.IO.File.AppendAllLines(@"d:\errors.txt", outputLines);
                }
                else
                {
                    System.IO.File.AppendAllLines(@"d:\errors.txt", outputLines);
                }
                throw;
            }
        }

        public void RemoveCliente(int id)
        {
            var existe = context.CLIENTEs.Find(id);

            if (existe != null)
            {
                context.CLIENTEs.Remove(existe);
                context.SaveChanges();
            }
        }

        public CLIENTE GetFromClienteByRuc(string ruc)
        {
            var query = from c in context.CLIENTEs
                        where c.Ruc.Equals(ruc)
                        select c;
            return query.SingleOrDefault();
        }
    }
}
